package cn.domypay.back.utils;

import java.sql.Timestamp;
import java.text.*;
import java.util.*;
/**
 *
 * <p>Title: </p>
 * <p>Description: Swanet common tools</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: Swanet </p>
 * @author Tim huang
 * @version 1.0
 */
public class DateUtil {
  public DateUtil() {
  }
  /**
   *
   * @param s String
   * @param pattern String
   * @return Date
   */
  public static Date parseStringToDate(String s, String pattern){
    Date date = new Date();
    if(s == null || pattern == null)
      return null;
    try{
      SimpleDateFormat formatter = new SimpleDateFormat(pattern);
      DateFormat dateFormat = formatter.getDateInstance();
      date =  dateFormat.parse(s);
    }catch(ParseException e){
      e.printStackTrace();
    }
    return date;
  }
  /**
   *
   * @param date Date
   * @param pattern String
   * @return String
   */
  public static String parseDateToString(Date date, String pattern){
    String strDate = null;
    String strFormat = pattern;
    SimpleDateFormat dateFormat = null;
    if(date == null){
      return "";
    }else{
      dateFormat = new SimpleDateFormat(strFormat);
      strDate = dateFormat.format(date);
      return strDate;
    }
  }
  /**
   *
   * @param date Date
   * @param pattern String
   * @param locale Locale
   * @return String
   */
  public static String parseDateToString(Date date, String pattern, Locale locale){
    String strDate = null;
    String strFormat = pattern;
    SimpleDateFormat dateFormat = null;
    if(date == null){
      return "";
    } else{
      dateFormat = new SimpleDateFormat(strFormat, locale);
      strDate = dateFormat.format(date);
      return strDate;
    }
  }
  /**
   *
   * @param date Date
   * @return Timestamp
   */
  public static Timestamp getTimestamp(Date date){
    if(date == null){
      return null;
    } else{
      Timestamp t = new Timestamp(date.getTime());
      return t;
    }
  }
  /**
   *
   * @param date Date
   * @return String
   */
  public static String getLocalString(Date date){
    DateFormat format = DateFormat.getDateInstance(1);
    return format.format(date);
  }
  /**
   *
   * @param date Date
   * @param locale Locale
   * @return String
   */
  public static String getLocalString(Date date, Locale locale){
    DateFormat format = DateFormat.getDateInstance(1, locale);
    return format.format(date);
  }
  /**
   * compose date
   * @param date Date
   * @param time Date
   * @return Date
   */
  public static Date composeDate(Date date, Date time){
    if(date == null || time == null){
      return null;
    } else{
      Calendar c1 = Calendar.getInstance();
      c1.setTime(date);
      Calendar c2 = Calendar.getInstance();
      c2.setTime(time);
      Calendar c3 = Calendar.getInstance();
      c3.set(c1.get(1), c1.get(2), c1.get(5), c2.get(11), c2.get(12), c2.get(13));
      return c3.getTime();
    }
  }
  /**
   *
   * @param date Date
   * @return Date
   */
  public static Date getTheDate(Date date){
    if(date == null){
      return null;
    } else{
      Calendar c1 = Calendar.getInstance();
      c1.setTime(date);
      Calendar c2 = Calendar.getInstance();
      c2.set(c1.get(1), c1.get(2), c1.get(5), 0, 0, 0);
      long millis = c2.getTimeInMillis();
      millis -= millis % 1000L;
      c2.setTimeInMillis(millis);
      return c2.getTime();
    }
  }
  /**
   *
   * @param d Date
   * @return Date
   */
  public static Date nextDay(Date d){
    if(d == null){
      return null;
    } else{
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(d);
      calendar.add(5, 1);
      return calendar.getTime();
    }
  }
  /**
   *
   * @param d Date
   * @param l long
   * @return Date
   */
  public static Date nextDay(Date d,int l){
	  if(d == null){
	      return null;
	    } else{
	      Calendar calendar = Calendar.getInstance();
	      calendar.setTime(d);
	      calendar.add(5, l);
	      return calendar.getTime();
	    }
  }
  /**
   *
   * @param d Date
   * @return Date
   */
  public static Date previousDay(Date d){
    if(d == null){
      return null;
    } else{
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(d);
      calendar.add(5, -1);
      return calendar.getTime();
    }
  }
  
  /**
   *
   * @param src String
   * @param length int
   * @param c char
   * @return String
   */
  public static String leftExpandString(String src, int length, char c){
    if(src == null)
      src = "";
    for(; src.length() < length; src = c + src);
    return src;
  }


  /**
   *
   * @param timezone int
   * @return String
   */
  public static String getTimezone(int timezone){
    if(timezone == 0)
      return "GMT 00:00";
    String prefix = null;
    String hour = null;
    String minute = null;
    int i = timezone;
    if(timezone > 0){
      prefix = "+";
    } else{
      prefix = "-";
      i = -timezone;
    }
    if(i % 10 == 5)
      minute = "30";
    else
      minute = "00";
    int h = i / 10;
    hour = Integer.toString(h);
    hour = leftExpandString(hour, 2, '0');
    String s = "GMT " + prefix + hour + ":" + minute;
    return s;
  }
  /**
   *
   * @param d1 Date
   * @param d2 Date
   * @return int
   */
  public static int compareTheTime(Date d1, Date d2){
    Date date = new Date();
    Date date1 = composeDate(date, d1);
    Date date2 = composeDate(date, d2);
    return date1.compareTo(date2);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getYear(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(1);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getMonth(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(2);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getDate(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(5);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getHour(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(11);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getMinute(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(12);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getSecond(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(13);
  }
  /**
   *
   * @param date Date
   * @return int
   */
  public static int getWeek(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(7);
  }
  /**
   *
   * @param date Date
   * @return Date
   */
  public static Date cutMilliSecond(Date date){
    long millis = date.getTime();
    millis -= millis % 1000L;
    return new Date(millis);
  }
  /**
   *
   * @param date Date
   * @return Date
   */
  public static Date roundLower(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int min = cal.get(12);
    if(min < 30)
      min = 0;
    else
      min = 30;
    cal.set(12, min);
    cal.set(13, 0);
    return cal.getTime();
  }
  /**
   *
   * @param date Date
   * @return Date
   */
  public static Date roundUpper(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(13, 0);
    int min = cal.get(12);
    if(min != 0)
      if(min <= 30){
        min = 30;
        cal.set(12, min);
      } else{
        min = 0;
        cal.set(12, min);
        cal.add(11, 1);
      }
    return cal.getTime();
  }
  /**
   *
   * @return Date
   */
  public static Date getCurrentDate(){
      Calendar calendar = Calendar.getInstance();
      TimeZone timezone = calendar.getTimeZone();
      int l = timezone.getRawOffset() / 0x36ee80;
      calendar.add(10, -1 * l);
      return calendar.getTime();
  }
  
  public static String getCurrentDateYYYYMMDD()
  {
      Calendar calendar=Calendar.getInstance();
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
      return sdf.format(calendar.getTime());
  }
  /**
   *
   * @return
   */
  public static String getCurrentDateString(){
      Calendar calendar = Calendar.getInstance();
      TimeZone timezone = calendar.getTimeZone();
      int l = timezone.getRawOffset() / 0x36ee80;
      calendar.add(10, -1 * l);
      int iMonth = calendar.get(Calendar.MONTH)+1;
      String month="";
      if(iMonth<10){
        month = "0" + iMonth;
      }else{
        month=""+iMonth;
      }
      int iDate = calendar.get(Calendar.DATE);
      String date="";
      if(iDate<10){
        date = "0" + iDate;
      }else{
        date=""+iDate;
      }
      int iHour =calendar.get(Calendar.HOUR_OF_DAY);
      String hour="";
      if(iHour<10){
        hour="0"+iHour;
      }else{
        hour=""+iHour;
      }
      int iMin=calendar.get(Calendar.MINUTE);
      String min="";
      if(iMin<10){
        min="0"+iMin;
      }else{
        min=""+iMin;
      }
      //cal.g
      return ""+calendar.get(Calendar.YEAR)+"-"+month+"-"+date+" "
          +hour+":"+min+":"
          +calendar.get(Calendar.SECOND);

  }
  public static String getSysDate() {
		// �õ�ϵͳ����
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ret = df.format(date.getTime());
		return ret;
	}
  
  public static Integer getIntDate()
  {
	  	Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("MMddHHmmssS");
		String ret = df.format(date.getTime());
		return Integer.valueOf(ret);
  }

}
